const vm = new Vue({
  el: '#app',
  data: {
    books: [
      { id: 1, name: '《算法导论》', date: '2006-9', price: 85.5, count: 1, isClick: false },
      { id: 2, name: '《UNIX编程艺术》', date: '2006-2', price: 59.0, count: 1, isClick: false },
      { id: 3, name: '《编程珠玑》', date: '2008-10', price: 39.0, count: 1, isClick: false },
      { id: 4, name: '《代码大全》', date: '2006-3', price: 128.0, count: 1, isClick: false }
    ]
  },
  methods: {
    // 减
    subCount(item) {
      // 需判断是否大于等于1
      if (item.count > 1) {
        item.count--
      } else if (item.count === 1) {
        item.count--
        item.isClick = true
      } else {
        item.count = 0
        item.isClick = true
      }
    },
    // 加
    addCount(item) {
      item.count++
      item.isClick = false
    },

    // 移除
    removeItem(index) {
      this.books.splice(index, 1)
    }
  },
  computed: {
    // 总价格
    totalPrice() {
      let totalPr = 0
      if (this.books.length) {
        this.books.map((item) => {
          totalPr += parseInt(item.price) * item.count
        })
      }
      return totalPr
    }
  },
  filters: {
    showPrice(price) {
      return '¥' + price.toFixed(2)
    }
  }
})
